package com.zhiyou.crowdfunding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiyou.crowdfunding.bean.Projects;
import com.zhiyou.crowdfunding.bean.User;
import com.zhiyou.crowdfunding.mapper.ProjectsMapper;
import com.zhiyou.crowdfunding.mapper.UserMapper;
import com.zhiyou.crowdfunding.service.ProjectsService;
import com.zhiyou.crowdfunding.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jyf
 * @since 2022-05-27
 */
@Service
public class ProjectsServiceImpl implements ProjectsService {

    @Resource
    private ProjectsMapper projectsMapper;

    @Autowired
    private UserService userService;

    /**
     * 查询所有项目，并且分页
     * @param pageNum  页数
     * @param pageSize  每页条数
     * @return
     */
    @Override
    public Page<Projects> getAllProjects(int pageNum,int pageSize) {
        Page<Projects> page = new Page<>(pageNum,pageSize);
        return projectsMapper.selectPage(page,null);
    }

    /**
     *
     * @param psPstId
     * @param pageNum
     * @param pageSize
     * @return
     *
     */
    @Override
    public Page<Projects> getProjectsByCondition(Integer psPstId,String psName,String usName,Integer psType, Integer pageNum, Integer pageSize) {
        Page<Projects> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Projects> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(psPstId)){
            queryWrapper.eq("ps_pst_id",psPstId);
        }
        if (!StringUtils.isEmpty(psName)){
            queryWrapper.like("ps_name",psName);
        }
        if (!StringUtils.isEmpty(psType)){
            queryWrapper.eq("ps_typ",psType);
        }
        if (!StringUtils.isEmpty(usName)){
            User user = userService.getUserByUsname(usName);
            queryWrapper.eq("ps_us_id",user.getUsId());
        }
        /*if (psPstId!=null || psType!=null || psName!=null){
            queryWrapper.eq("ps_pst_id",psPstId).or().
                    eq("ps_name",psName).or().
                    eq("ps_type",psType);
        }*/
        return projectsMapper.selectPage(page,queryWrapper);

    }
}
